// serialize.cpp
#include "serialize.h"
#include "../misc.h"

int ISerilize::ToBytes(STD_BUFFER& bufOut)
{
    int iResult, iTmp;

    iResult = ToByteSize();
    if(iResult < 0) return iResult;

    bufOut.resize(iResult);
    iTmp    = iResult;
    iResult = ToBytes(bufOut.data(), iTmp);
    bufOut.resize(iTmp);

    return iResult;
}

int SAPI_U64ToBytes(U64 u64Data, BYTE* pOut, int iLE)
{
    BYTE* p = (BYTE*)&u64Data;

    if(STDEXAPI_IsLE() == iLE) memcpy(pOut, p, sizeof(U64));
    else
        for(int i = 0; i < 8; i++) pOut[i] = p[7 - i];

    return 0;
}

U64 SAPI_U64FromBytes(const BYTE* data, int iLE)
{
    U64 u64Ret;
    BYTE* p = (BYTE*)&u64Ret;

    if(STDEXAPI_IsLE() == iLE) memcpy(p, data, sizeof(U64));
    else
        for(int i = 0; i < 8; i++) p[i] = data[7 - i];

    return u64Ret;
}
